package com.jksoft.darewayyyy.util;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/6.
 */
public class NetUtil {

    public static Map get(String url) {
        final Map map = new HashMap<String, Object>();
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.GET,
                url,
                new RequestCallBack<Map>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }

                    @Override
                    public void onSuccess(ResponseInfo<Map> responseInfo) {
                        LogUtil.i("NetUtil", responseInfo.result.toString());
                        map.put("name", responseInfo.result.get("name"));
                    }

                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                    }
                });
        return map;
    }

    public void post(String url, Map map){
        RequestParams params = new RequestParams();
//        params.addHeader("name", "value");
//        params.addQueryStringParameter("name", "value");
        String empId = (String) map.get("empId");
// 只包含字符串参数时默认使用BodyParamsEntity，
// 类似于UrlEncodedFormEntity（"application/x-www-form-urlencoded"）。
        params.addBodyParameter("empId", empId);

// 加入文件参数后默认使用MultipartEntity（"multipart/form-data"），
// 如需"multipart/related"，xUtils中提供的MultipartEntity支持设置subType为"related"。
// 使用params.setBodyEntity(httpEntity)可设置更多类型的HttpEntity（如：
// MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity）。
// 例如发送json参数：params.setBodyEntity(new StringEntity(jsonStr,charset));
//        params.addBodyParameter("file", new File("path"));
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST,
                url,
                params,
                new RequestCallBack<String>() {

                    @Override
                    public void onStart() {
//                        testTextView.setText("conn...");
                    }

                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                        if (isUploading) {
//                            testTextView.setText("upload: " + current + "/" + total);
                        } else {
//                            testTextView.setText("reply: " + current + "/" + total);
                        }
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
//                        testTextView.setText("reply: " + responseInfo.result);
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
//                        testTextView.setText(error.getExceptionCode() + ":" + msg);
                    }
                });
    }

    public void download(String url, String savePath){
        HttpUtils http = new HttpUtils();
        // url:"http://apache.dataguru.cn/httpcomponents/httpclient/source/httpcomponents-client-4.2.5-src.zip",
        // "/sdcard/httpcomponents-client-4.2.5-src.zip"
        HttpHandler handler = http.download(url, savePath,
                true, // 如果目标文件存在，接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。
                true, // 如果从请求返回信息中获取到文件名，下载完成后自动重命名。
                new RequestCallBack<File>() {

                    @Override
                    public void onStart() {
//                        testTextView.setText("conn...");
                    }

                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
//                        testTextView.setText(current + "/" + total);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<File> responseInfo) {
//                        testTextView.setText("downloaded:" + responseInfo.result.getPath());
                    }


                    @Override
                    public void onFailure(HttpException error, String msg) {
//                        testTextView.setText(msg);
                    }
                });

    }
}
